#include <stdint.h>
#include "framework.h"
#include "main.h"

#if SOC_CONFIG_INTERRUPT_OVERRIDE_EN > 0

//处理中断
extern "C"
{
	
//外部中断
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
#define CASE(n)  case GPIO_PIN_##n: IF_SOC_Exti::RaiseExit_IRQ_Global(GpioExtiDevId::Line##n); break

    switch(GPIO_Pin)
    {
        CASE(0);
        CASE(1);
        CASE(2);
        CASE(3);
        CASE(4);
        CASE(5);
        CASE(6);
        CASE(7);
        CASE(8);
        CASE(9);
        CASE(10);
        CASE(11);
        CASE(12);
        CASE(13);
        CASE(14);
        CASE(15);
    }

#undef CASE
}

#ifdef HAL_UART_MODULE_ENABLED 
/// @brief 定时器计数中断
/// @param htim 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    auto ptr = IF_SOC_Timer::GetInstanceByHandle(htim);
    if(ptr)
    {
        ptr->RaiseElapsed_IRQ();
    }
}

/// @brief 定时器匹配中断
/// @param htim 
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
    auto ptr = IF_SOC_Timer::GetInstanceByHandle(htim);
    if(ptr == nullptr)
        return;

    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
    {
        ptr->RaiseOc_IRQ(1);
    }
    else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
    {
        ptr->RaiseOc_IRQ(2);
    }
    else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
    {
        ptr->RaiseOc_IRQ(3);
    }
    else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
    {
        ptr->RaiseOc_IRQ(4);
    }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    auto ptr = IF_SOC_Uart::GetInstanceByHandle(huart);
    if(ptr)
    {
        ptr->RaiseDataReceivedCallBack();
    }
}

//RECV IDLE
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    auto ptr = IF_SOC_Uart::GetInstanceByHandle(huart);
    if(ptr)
    {
        ptr->RaiseIdleEventCallBack();
    }
}

#endif 

#ifdef HAL_ADC_MODULE_ENABLED 
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    auto ptr = IF_SOC_Adc::GetInstanceByHandle(hadc);
    if(ptr)
    {
        ptr->Raise_ConvertOver_IRQ();
    }
}
#endif 

}

#endif 